/**
 * 
 */
(function($){
	$(document).delegate(".modal-form","submit",function(){
		var cb = $(this).attr("callback");
		
		$(this).ajaxSubmit(function(rsp){
			if(rsp.code===0){
				if(cb){
					if(window[cb])
						window[cb](rsp);
				}
				return;
			}
			alert(rsp.msg||"操作失败!");
			
		});
		return false;
	});
	
	//所有的异步的按钮的单机事件
	$(document).delegate(".btn-ajax","click",function(){
		var url = $(this).attr("ajax-url");
		var cb = $(this).attr("callback");
		$.get(url,function(rsp){
			if(rsp.code===0){
				if(cb){
					window[cb](rsp);
				}else{
					alert("操作成功!");
				}
				return;
			}
			alert(rsp.msg||"操作失败!");
		});
		return false;
	});
	
	$(document).delegate(".cbx_YON","click",function(){
		$(this).prev().val($(this).prop('checked')?1:0);
	});
	
	$(".cbxYON").each(function(){
		var $parent = $(this).parent();
		var v = $(this).val();
		var $cbxYON = $('<input type="checkbox" class="cbx_YON" />');
		$cbxYON.prop('checkbox',v==1);
		$parent.append($cbxYON);
		
	});
	
	
	//初始化datagrid
	
	
})(jQuery);

//日期时间的扩展
Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
		"S" : this.getMilliseconds()
	// millisecond
	}
	if (/(y+)/.test(format))
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
	return format;
}


window.formatter = {
	yon : function(value){
		return value==1?"是":"否";
	}	,
	datetime:function(value){
		return new Date(value).format('yyyy-MM-dd hh:mm:ss');
	}
};
//datagrid插件
(function($) {
	if ($ == undefined) {
		if (console.log) {
			console.log('jQuery Required!');
		}
		alert('jQuery Required!');
		return false;
	}

	var defaults = {};

	//上传图片的插件
	//this->指向需要打开上传对话况的按钮
	$.fn.datagrid = function(options) {
		var opts = $.extend(defaults, options);
		var $datagrid = $(this).find(".datagrid");
		$(this).find(".remove-bar").click(function() {
			var selects = $datagrid.bootstrapTable('getSelections');
			if (selects.length > 0) {
				if (!confirm("确认删除么?")) {
					return false;
				}
			}
			var ids = $.map(selects, function(row) {
				return row.id;
			});
			var url = $(this).attr("target-url");
			$.post(url + '?id[]=' + ids, function(rsp) {
				if(rsp.code===0){
					$datagrid.bootstrapTable('remove', {
						field : 'id',
						values : ids
					});
				}else{
					alert('删除失败!');
				}
				
			});
		});
		$datagrid.bootstrapTable({
			onDblClickRow : function(row) {
				//alert(JSON.stringify(row));
				//可以进行编辑
				$editForm = $("form.edit-form");
				$editForm.find("#rowid").val(row.id);
				for ( var k in row) {
					var $c = $editForm.find("*[name='" + k + "']");//获取相应的控件
					if (!$c) continue;
					$c.val(row[k]);
					//如果是是否单选框需要作js的操作
					if($c.hasClass("cbxYON")){
						$c.next().prop('checked',row[k]==1);
					}
				}
				$("#edit-form-dialog").modal('show');
			}
		});
		
		return $datagrid;
	};
})(jQuery);    


